package com.zrar.ak.gkpt.service.impl.record;

import com.zrar.ak.gkpt.dao.IBusinessContractManageDao;
import com.zrar.ak.gkpt.model.bo.BusinessContractManageBO;
import com.zrar.ak.gkpt.model.vo.UserVO;
import com.zrar.ak.gkpt.service.record.IBusinessOntractInfoService;
import com.zrar.ak.gkpt.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: BusinessOntractInfoService
 * @Description: 合同管理Service实现类
 * @Author: CaoTao
 * @Create: 2020/04/07
 */
@Service
public class BusinessOntractInfoService implements IBusinessOntractInfoService {

    @Autowired
    IBusinessContractManageDao contractManageDao;


    /**
     * 保存合同信息
     *
     * @param jsonResult
     * @param formDataJson
     * @param userVo
     * @return
     */
    @Override
    public JsonResult OntractInfoSave(JsonResult jsonResult, String formDataJson, UserVO userVo) {
        BusinessContractManageBO businessContractManageBO = new BusinessContractManageBO();
        try {
            businessContractManageBO = CommonTools.jsonStrToObject(formDataJson, BusinessContractManageBO.class);
            String unitName = businessContractManageBO.getUnitName();
            // 合同信息已经存在则不能新增
            BusinessContractManageBO businessContractManageBO1 = contractManageDao.queryContractByUnitName(unitName);
            if (businessContractManageBO1 != null) {
                CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_EXISTENT_DATA);
            } else {
                String creatorId = userVo.getUserId();
                Date createTime = new Date();
                String updateId = userVo.getUserId();
                Date updateTime = new Date();
                businessContractManageBO.setId(CommonTools.getUUID());
                businessContractManageBO.setCreatorId(creatorId);
                businessContractManageBO.setCreateTime(createTime);
                businessContractManageBO.setUpdateUserId(updateId);
                businessContractManageBO.setUpdateTime(updateTime);
                businessContractManageBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
                int rowNum = contractManageDao.insert(businessContractManageBO);
                if (rowNum > 0) {
                    CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_SUCCESS);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_BUSINESS_EXCEPTION);
        }
        return jsonResult;
    }

    /**
     * 根据合同编号删除合同信息
     *
     * @param jsonResult
     * @param id
     * @return
     */
    @Override
    public JsonResult deleteOntractInfoByontractId(JsonResult jsonResult, String id) {
        int rowNum = contractManageDao.deleteOntractById(id);
        jsonResult = CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_SUCCESS);
        return jsonResult;
    }

    /**
     * 更新合同信息
     *
     * @param jsonResult
     * @param formDataJson
     * @param userVo
     * @return
     */
    @Override
    public JsonResult OntractInfoUpdate(JsonResult jsonResult, String formDataJson, UserVO userVo) {
        BusinessContractManageBO businessContractManageBO = new BusinessContractManageBO();
        String updateUserId = userVo.getUserId();
        Date updateTime = new Date();
        try {
            businessContractManageBO = CommonTools.jsonStrToObject(formDataJson, BusinessContractManageBO.class);
            BusinessContractManageBO contractBO = contractManageDao.selectByPrimaryKey(businessContractManageBO.getId());
            if (contractBO == null || BusinessEnum.IS_DELETE_Y.getValue().equals(contractBO.getIsDelete())) {
                CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_NONEXISTENT_DATA);
            } else if (!contractBO.getUnitId().equals(businessContractManageBO.getUnitId())) {
                //如果同一主键的数据库表记录的单位ID和传入数据的单位ID值不一样，则返回业务异常错误
                CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_BUSINESS_EXCEPTION);
            } else {
                businessContractManageBO.setCreatorId(contractBO.getCreatorId());
                businessContractManageBO.setCreateTime(contractBO.getCreateTime());
                businessContractManageBO.setUpdateUserId(updateUserId);
                businessContractManageBO.setUpdateTime(updateTime);
                businessContractManageBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
                contractManageDao.updateByPrimaryKey(businessContractManageBO);
                CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_SUCCESS);
            }
        } catch (IOException e) {
            e.printStackTrace();
            CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_BUSINESS_EXCEPTION);
        }
        return jsonResult;
    }

    /**
     * 根据合同编号查询合同信息
     *
     * @param jsonResult
     * @param id         主键
     * @return
     */
    @Override
    public JsonResult queryOntractInfoById(JsonResult jsonResult, String id) {
        BusinessContractManageBO contractBO = new BusinessContractManageBO();
        //ContractBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
        //ContractBO.setContractCode(id);
        contractBO = contractManageDao.selectByPrimaryKey(id);
        jsonResult.setData(contractBO);
        CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_SUCCESS);
        return jsonResult;
    }

    /**
     * 分页
     *
     * @param jsonResult
     * @param currentPage
     * @param pageSize
     * @param unitName
     * @return
     */
    @Override
    public JsonPageResult getOntractInfoList(JsonPageResult jsonResult, int currentPage, int pageSize, String unitName,
                                             String contractCode) {
        if (currentPage <= 0) {
            currentPage = 1;
        }
        if (StringUtils.isEmpty(unitName)) {
            unitName = null;
        }
        if (StringUtils.isEmpty(contractCode)) {
            contractCode = null;
        }
        Map map = new HashMap();
        map.put("currentPage", currentPage);
        map.put("pageSize", pageSize);
        map.put("unitName", unitName);
        map.put("contractCode", contractCode);
        //返回前端的具体的数据
        List list = null;
        //符合展示条件的数据总数
        Integer total = null;
        total = contractManageDao.queryContractInfoShowNum(map);
        list = contractManageDao.pagingQueryContractInfoList(map);
        if (total == null) {
            total = 0;
        }
        //计算一共多少页
        Integer pageNum = (total % pageSize == 0) ? (total / pageSize) : (total / pageSize + 1);

        jsonResult.setData(list);
        jsonResult.setCurrentPage(currentPage);
        jsonResult.setPageSize(pageSize);
        jsonResult.setTotalCount(total);
        jsonResult.setTotalPage(pageNum);
        if (list == null || list.size() == 0) {
            CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_NONEXISTENT_DATA);
        } else {
            CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_SUCCESS);
        }
        return jsonResult;
    }
}
